// Copyright (C) 2010-2012, MaGUI
//
//  Elena Ballesteros Molinero
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Soporte;

namespace Magui
{
    public partial class FormLupa : Form
    {
        
        private Timer _tmrRefresh;

        private static FormLupa _instance;
        public static FormLupa Instance
        {
            get
            {
                if(_instance == null) 
                    _instance = new FormLupa();
                return _instance;
            }
        }

        private FormLupa()
        {
            CheckForIllegalCrossThreadCalls = false;

            InitializeComponent();
         
            _tmrRefresh = new Timer
                              {
                                  Interval = 1000/60,
                                  Enabled = false
                              };
            _tmrRefresh.Tick += tmrRefresh_Tick;

            Controlador.Instance.SetLayeredForm(this.Handle);
        }        

        void tmrRefresh_Tick(object sender, EventArgs e)
        {
            Controlador.Instance.Point = Controlador.Instance.UpdatePosition();
            if (Controlador.Instance.Point != Point.Empty)
            {
               Magnificador.Instance.Repintar(Controlador.Instance.Point.X, Controlador.Instance.Point.Y, false);
            }
            try
            {
                Refresh();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());                
            }
            
        }
        
        private void FormLupa_Paint(object sender, PaintEventArgs e)
        {            
            Graphics g = e.Graphics;

            if(g!= null)
            {                
                try
                {
                    /*g.SmoothingMode = SmoothingMode.HighSpeed;
                    g.CompositingQuality = CompositingQuality.HighSpeed;
                    g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
                    g.InterpolationMode = InterpolationMode.Low;
                     */

                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    Magnificador.Instance.CreaGraphics(g, Controlador.Instance.PointMouse);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

                // Dibuja el borde de la ventana
                Borde borde = Magnificador.Instance.Contenedor.Borde;
                g.DrawRectangle(new Pen(borde.Color, (int)borde.Grosor),
                                0, 0, Width, Height);
            }            
        }        

        private void FormLupa_Load(object sender, EventArgs e)
        {
            DoubleBuffered = true;
        }        
                
        private void FormLupa_VisibleChanged(object sender, EventArgs e)
        {
            _tmrRefresh.Enabled = Visible;
        }
    }
}